<html>
	<head>
		<title>JavaScript Test Page</title>
	</head>
	<body>
		<script language="JavaScript" type="text/javascript" src="http://localhost/~mferrare/gfportfoliogadget/Portfolio.js"></script>
		<script type="text/javascript" language="JavaScript">
			// Javascript will go here

			// Use this data for testing at the moment until we work out a way to get it from HTML
			// This is a dump from this URL:
			// http://www.google.com/finance/getprices?q=3383&x=HKG&p=6Y&i=604800&f=d,c,v&df=epoch
			// First part is the date, second is the price on that date, third is volume.
			// In the URL the parameters are:
			// q	Stock index (3383 is Agile property on Hong Kong Exchange)
			// x	Stock Exchange (HKG for Hong Kong)
			// p	Period - 6Y is 6 years
			// i 	Interval - 604800 is number of seconds in a week
			// f	Output format.  In this case date, closing price and volume
			// df	I'm not sure.  I don't know what 'epoch' means either.
			// There's no documentation from google on any of this.
			var testData = "1134720000,3.75,706357500\n1135324800,3.65,235633207\n1135929600,3.75,70658000\n1136534400,4.075,188042037\n1137139200,4.225,196295000\n1137744000,4.5,181422000\n1138348800,4.625,108882618\n1138953600,4.45,73017000\n1139558400,4.4,100249305\n1140163200,4.55,61993000\n1140768000,4.55,60498909\n1141372800,4.55,72452000\n1141977600,4.5,61915000\n1142582400,4.975,89508500\n1143187200,5.5,100087000";

			var myPortfolio = new Portfolio();
			myPortfolio.addSymbol("3383");
			myPortfolio.displaySymbols();
			myPortfolio.addSymbol("3384");
			myPortfolio.displaySymbols();
			myPortfolio.addSymbol("3385");
			myPortfolio.displaySymbols();
			myPortfolio.addSymbol("3385");
			myPortfolio.displaySymbols();
			var dataLines = testData.split('\n');
			for (var i in dataLines) {
				var singleLine = dataLines[i].split(',');
				var shortDate = printShortDate(getDate(singleLine[0]));
				debugOutput("Date: " + shortDate + " Price: " + singleLine[1])
			}


			


			function debugOutput(text) {
			// Expect: text to print
			// Return: Nothing
			// Prints the text as debug text in the HTML output
				document.write("Debug: " + text + "<br>\n");
			}

			function getDate(date) {
			// Expect: String from Google Finance date
			// Return: Date object
			// Converts the date string received from the GF stream into a readable and parseable
			// date object.  Simply multiplies the value by 1000.  Then we can format it using the
			// Date object routines
				return new Date(date * 1000);
			}

			function printShortDate(date)
			// Expect: Date Object
			// Return: String containing short date formatted as yyyy/mm/dd
			{
				// For some reason I can't do this inline so I'll split it out
				var month = date.getMonth();
				month++;

				return date.getFullYear() + "/" + month + "/" + date.getDate();
			}


		</script>
	</body>
</html>
